package org.tictactoe;

import org.omg.CORBA.*;
import org.tictactoe.corba.*;
import org.tictactoe.*;
import java.util.Map;
import java.util.HashMap;

public class TictactoeServiceImpl extends TictactoeServicePOA
{
	private ORB orb;
	private int playerCount;
	private int gameCount;
	private Map<Integer, Game> gameMap = new HashMap<Integer, Game>();
	Game currentGame;
	
	public TictactoeServiceImpl() {
		playerCount = 0;
		gameCount = 0;
	}
	
	public Game startGame(IntHolder team) {
		if (playerCount == 0) {
			TictactoeGame gameServant = new TictactoeGame(gameCount);
			try {
				currentGame = GameHelper.narrow(_default_POA().servant_to_reference(gameServant));
			} 
			catch (Exception e) {
				e.printStackTrace();
			}			
			gameMap.put(gameCount, currentGame);
			playerCount += 1;
			team = new IntHolder(playerCount);
		} else {
			team = new IntHolder(playerCount);
			playerCount = 0;
			gameCount += 1;
			currentGame.start();
		}
		return currentGame;
	}
	
	public void endGame(int id) {
		if (gameMap.containsKey(id)) {
			gameMap.remove(id);
		}
	}

	
}
